<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\RechargePackage;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class RechargePackageController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new RechargePackage(), function (Grid $grid) {
            $grid->model()->with(['operator']);
            $grid->model()->with(['recharge_type']);
            $grid->model()->orderByDesc('created_at');
            $grid->quickSearch('title');

            $grid->selector(function (Grid\Tools\Selector $selector) {

                $operators = \DB::table('operator')->pluck('name')->toArray();
                $selector->select('operator', '运营商', array_combine($operators, $operators), function ($query, $value) {
                    $query->whereHas('operator', function ($q) use ($value) {
                        $q->where('name', current($value));
                    });
                });

                $options = \DB::table('recharge_type')->pluck('name')->toArray();
                $selector->select('recharge_type', '类型', array_combine($options, $options), function ($query, $value) {
                    $query->whereHas('recharge_type', function ($q) use ($value) {
                        $q->where('name', current($value));
                    });
                });

                $selector->select('is_on', '是否启用', [
                    1 => '已启用',
                    0 => '未启用'
                ]);

                $selector->select('recommend', '是否推荐', [
                    1 => '已推荐',
                    0 => '未推荐'
                ]);
            });

            $grid->id->sortable();
            $grid->title->editable();
            $grid->is_on->switch();
            $grid->recommend->switch();
            $grid->valid_days->sortable();
            $grid->column('recharge_type.name');
            $grid->column('money')->display(function ($v) {
                if($this->money_tax) {
                    return '฿'.finance_format($this->money_tax);
                } else {
                    return '฿'.finance_format($v);
                }
            })->sortable();
            $grid->description
                ->if(function ($description) {
                    return $description->getValue();
                })
                ->then(function (Grid\Column $column) {
                    $column->display(function() {
                        if($this->description) {
                            return '查看';
                        }
                    }) // 设置按钮名称
                    ->modal(function ($modal) {
                        $modal->title($this->title);
                        return $this->description;
                    });
                });
            $grid->column('operator.name');
            $grid->order->sortable();
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new RechargePackage(), function (Show $show) {
            $show->id;
            $show->title;
            $show->recharge_type_id;
            $show->money;
            $show->description;
            $show->operator_id;
            $show->order;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new RechargePackage(), function (Form $form) {
            // $form->display('id');

            $form->tab('基本信息', function (Form $form) {
                $form->select('operator_id')->options(function () {
                    return \DB::table('operator')->pluck('name','id');
                })->required();
                $form->select('recharge_type_id')->options(function () {
                    return \DB::table('recharge_type')->pluck('name','id');
                })->required();
                $form->text('title')->required();
                $form->text('money')->customFormat(function ($v) {
                    return finance_format($v);
                })->help('泰铢')->required();
                $form->text('money_tax')->customFormat(function ($v) {
                    return finance_format($v);
                })->help('如果价格没有包含增值税，才填写');
                $form->switch('is_on')->saving(function ($v) {
                    return $v ? 1 : 0;
                })->default(1);
                $form->switch('recommend')->saving(function ($v) {
                    return $v ? 1 : 0;
                })->default(0);
                $form->text('order')->default(0);
            })->tab('套餐信息', function (Form $form) {
                $form->text('valid_days')->help('有效期/天');
                $form->text('max_bandwidth')->help('最快速度，没有限制就填max');
                $form->text('min_bandwidth')->help('超过流量以后的速度，如果超过流量就不能用了，就填0');
                $form->text('max_flow')->help('最大流量，没有限制就填max，不能上网就写0');
                $form->textarea('description');
                $form->textarea('remarks');
            });


            // $form->display('created_at');
            // $form->display('updated_at');
        });
    }
}
